package de.szut.impl;

import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;
import de.szut.entity.Modifier;
import de.szut.entity.Points;
import de.szut.interfaces.PointsDao;

import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: dzebcuks
 * Date: 11.10.13
 * Time: 11:37
 * To change this template use File | Settings | File Templates.
 */
public class PointsDaoImpl extends BaseDaoImpl<Points, String> implements
        PointsDao {

    /**
     * Must be defined...
     *
     * @param connectionSource
     * @param dataClass
     * @throws java.sql.SQLException
     */
    public PointsDaoImpl(ConnectionSource connectionSource,
                           Class<Modifier> dataClass) throws SQLException {
        super(connectionSource, Points.class);
    }

    @Override
    public Points getPointsForCurrentUser() throws SQLException {
        QueryBuilder queryBuilder = this.queryBuilder();
        queryBuilder.orderBy("id", false);
        queryBuilder.limit(1);
        return (Points) queryBuilder.queryForFirst(); // TODO not sure, if this will work, if it doesnt just return the first entry of the .query() list
    }

    @Override
    public void savePoints(Points points) {
        try {
            this.update(points);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    @Override
    public void insertPoints(Points points){
        try {
            this.create(points);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
